package com.example.infowall02.mapper;

import com.example.infowall02.domain.Articles;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface FavoriteMapper {
    //增加收藏
    @Insert("INSERT INTO favorite (user_id, article_id) VALUES (#{userId}, #{articleId})")
    int addFavorite(@Param("userId") int userId, @Param("articleId") int articleId);
    //删除收藏
    @Delete("DELETE FROM favorite WHERE user_id = #{userId} AND article_id = #{articleId}")
    int deleteFavorite(@Param("userId") int userId, @Param("articleId") int articleId);
    //根据用户ID和文章ID查询是否收藏文章
    @Select("SELECT COUNT(*) FROM favorite WHERE user_id = #{userId} AND article_id = #{articleId}")
    int countFavorites(@Param("userId") int userId, @Param("articleId") int articleId);
    //根据用户ID查询收藏的文章列表
    // 获取用户点赞的文章列表
    @Select("SELECT article_id AS id, type, theme, rule, date, location, sign_method as signMethod, origin, likes, clicks, favorites, image, content FROM articles" +
            " where article_id IN (SELECT article_id FROM `favorite` WHERE user_id = #{userId})"+
            " order by id desc"
    )
    List<Articles> getUserFavoriteArticles(@Param("userId") int userId);
    //根据用户ID查询收藏的文章数量
    @Select("SELECT COUNT(*) FROM favorite WHERE user_id = #{userId}")
    int getUserFavoriteCount(@Param("userId") int userId);
}
